package matImpl;

import java.io.IOException;

import test.RunningTime;
import uci.rt.ccr.engine.matrix.Mat;
import cern.colt.matrix.impl.DenseDoubleMatrix2D;
import cern.colt.matrix.linalg.Algebra;

public class ColtMatTest
{
	public static void main(String[] args) throws IOException, InterruptedException 
	{
		/*int d = 3;	
		int k=1;
		double [][] data = new double[d][d];
		for(int r=0;r<d;r++){
			for (int c=0;c<d;c++){
				data[r][c]=k++;
			}
		}
		Mat a = new Mat(data);
		Mat b = a.transpose();
		DenseDoubleMatrix2D aColt = new DenseDoubleMatrix2D(data);
		DenseDoubleMatrix2D bColt = new DenseDoubleMatrix2D(b.getData());
		Mat c= a.multiplyByTranspose(b);

		Algebra algebra = new Algebra();
		DenseDoubleMatrix2D cColt=(DenseDoubleMatrix2D) algebra.mult(aColt, aColt);
		a.print();
		b.print();
		c.print();
		System.out.println(cColt);*/
		
		
		Mat a =  Mat.fillNmatix(5000, 750, Math.log(5));
		Mat b =  Mat.fillNmatix(5000, 750, Math.PI);
		Mat c = null;
		

		DenseDoubleMatrix2D aColt = new DenseDoubleMatrix2D(a.getData());
		DenseDoubleMatrix2D bColt = new DenseDoubleMatrix2D(b.getData());
		DenseDoubleMatrix2D cColt = new DenseDoubleMatrix2D(750,750);
		

		//C = A x B; Equivalent to A.zMult(B,C,1,0,false,false)

		int times = 10;
		
		int dimension = 100;
		Mat stat = new Mat(new double[3][dimension]);

		System.out.println("Start");
		RunningTime rt = new RunningTime();
	/*	
		for (int i = 0; i < times; i++)
		{
			rt.start();
			c=a.multiplyByTranspose(b);
			rt.stop();
			stat.set(0, i, rt.getElapsedMillisecs());
			System.out.println("multiplyByTranspose ->"+i);
		}
		stat.writeAsCsvFile("algebraVSMat_5000x750.csv", ",");
		
		
		for (int i = 0; i < times; i++)
		{
			rt.start();
			aColt.zMult(cColt, bColt);	
			rt.stop();
			stat.set(1, i, rt.getElapsedMillisecs());
			System.out.println("zMult ->"+i);
		}
		stat.writeAsCsvFile("algebraVSMat_5000x750.csv", ",");
		*/
		          
		Algebra algebra = new Algebra(1e-9);
		bColt=(DenseDoubleMatrix2D) bColt.viewDice();
		for (int i = 0; i < times; i++)
		{
			rt.start();
			DenseDoubleMatrix2D dColt=(DenseDoubleMatrix2D) algebra.mult(aColt, bColt);
			rt.stop();
			stat.set(0, i, rt.getElapsedMillisecs());
			System.out.println("algebra.mult ->"+i+"  t="+rt.getElapsedMillisecs());
		}
		stat.writeAsCsvFile("algebra_p=1e-9_5000x750.csv", ",");
		System.out.println("END");
		/*int dim=3;
		double[][] data1 = new double[dim][dim];
		int val = 1;
		for (int i = 0; i < dim; i++)
		{
			for (int j = 0; j < dim; j++)
			{
				data1[j][i] = val++;
			}
		}
		Mat a = new Mat(data1);
		//a.print();
		Mat b = a.multiplyByTranspose(a);
		Mat c = a.transpose();
		Mat e = a.multiply(c);
//		b.writeAsCsvFile("b.csv", ",");
//		e.writeAsCsvFile("e.csv", ",");
		//b.print();
		//e.print();
		DenseDoubleMatrix2D aColt = new DenseDoubleMatrix2D(data1);
		DenseDoubleMatrix2D eColt = new DenseDoubleMatrix2D(dim,dim);
		DenseDoubleMatrix2D cColt=(DenseDoubleMatrix2D) aColt.viewDice();
		//System.out.println(aColt);
		//System.out.println(cColt);
		//C = A x B; Equivalent to A.zMult(B,C,1,0,false,false)
		
		aColt.zMult(cColt, eColt);		
		//System.out.println(eColt );
		Mat eColtMat =  new Mat(eColt.toArray());

		System.out.println("Mat == ColtMat --> "+e.isEqual(eColtMat));

		System.out.println("-------------------");
		System.out.println("speed test");

		int k=0;
		int topDim = 2000;
		int times = 20;
		
		int dimension = 100;
		Mat stat = new Mat(new double[3][dimension]);
		Mat mat = new Mat(new double[1][times]);
		RunningTime rt = new RunningTime();

		for (int d = 50; d < topDim; d = d + 50)
		{
			double[][] data = new double[d][d];
			val = 0;
			for (int i = 0; i < d; i++)
			{
				for (int j = 0; j < d; j++)
				{
					data[j][i] = val++;
				}
			}
			DenseDoubleMatrix2D aa = new DenseDoubleMatrix2D(data);
			DenseDoubleMatrix2D cc = new DenseDoubleMatrix2D(d,d);

			for (int i = 0; i < times; i++)
			{
				rt.start();
				aa.zMult(aa, cc);
				rt.stop();
				mat.set(0, i, rt.getElapsedMillisecs());
			}
			stat.set(0, k, d);
			stat.set(1, k, mat.mean());
			stat.set(2, k, mat.standardDev());
			stat.writeAsCsvFile("multiplyColtStatistics.csv", ",");
			System.out.println(k+")dim: "+d);
			k++;
		}
		
		

		for (int d = 50; d < topDim; d = d + 50)
		{
			double[][] data = new double[d][d];
			val = 0;
			for (int i = 0; i < d; i++)
			{
				for (int j = 0; j < d; j++)
				{
					data[j][i] = val++;
				}
			}
			Mat aaMat = new Mat(data);
			Mat ccMat = null;

			for (int i = 0; i < times; i++)
			{
				rt.start();
				ccMat=aaMat.multiplyByTranspose(aaMat);
				rt.stop();
				mat.set(0, i, rt.getElapsedMillisecs());
			}
			stat.set(0, k, d);
			stat.set(1, k, mat.mean());
			stat.set(2, k, mat.standardDev());
			stat.writeAsCsvFile("multiplyByTransposeStatistics.csv", ",");
			System.out.println(k+")dim: "+d);
			k++;
		}
/**/
	}
}
